package com.allwees.bs.c.module.user.service.impl;

import com.allwees.bs.c.module.user.dto.MessageDto;
import com.allwees.bs.c.module.user.entity.MessageEntity;
import com.allwees.bs.c.module.user.mapper.MessageMapper;
import com.allwees.bs.c.module.user.repository.MessageRepository;
import com.allwees.bs.c.module.user.service.IMessageService;
import com.allwees.bs.core.modelbase.page.PageParams;
import com.allwees.bs.core.modelbase.page.PaginateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements IMessageService {

	@Autowired
	private MessageRepository messageRepository;

	@Autowired
	private MessageMapper messageMapper;

	@Override
	public PaginateDTO<MessageDto> getUserMessage(String userUuid, PageParams pageParams) {
		Page<MessageEntity> page = messageRepository.findByPage(userUuid, pageParams.toPageable(Direction.DESC, "id"));
		return messageMapper.entity2Dto(page, pageParams);
	}

	@Override
	public MessageEntity read(String messageUuid) {
		MessageEntity message = messageRepository.getByUuid(messageUuid);
		message.setMarkread(true);
		messageRepository.save(message);
		return message;
	}
}
